/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package BD.Modulos;
import BD.Bd;
import BD.ConnBdType;
import Interfaces.BdException;
import Objects.ItemLeftBar;
import Objects.ItemPrintScreen;
import Objects.Sesion;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
/**
 *
 * @author armgarces
 */
public class BdGui extends Bd{
    private String mensj;
    /***************************************************************************/
    /********************************** BD *************************************/
    /***************************************************************************/
    /**
     * 
     */
    public BdGui(){
       
    }    
    /***************************************************************************/
    /******************************** LEFTBAR **********************************/
    /***************************************************************************/    
    /**
     * 
     * @param value
     * @return
     * @throws SQLException 
     * @throws Interfaces.BdException 
     */
    public ItemLeftBar[] loadMenu(Sesion value) throws SQLException, BdException {
        try{
            Bd bd = ConnBdType.open(ConnBdType.SqlServer);
            connection = bd.open();
            if (connection != null){
                StringBuilder sqlProc = new StringBuilder();
                sqlProc.append("{call sp_get_menu_left(?)}");
                CallableStatement cstmt = connection.prepareCall(sqlProc.toString());
                cstmt.setObject("@id_user", value.getId());               
                ResultSet result = cstmt.executeQuery();            
                List<ItemLeftBar> list = new ArrayList<>();
                while(result.next()) {
                   ItemLeftBar obj = new ItemLeftBar();
                   obj.loadResultSet(result);
                   list.add(obj);
                }               
                return list.toArray(new ItemLeftBar[list.size()]);               
            }else{
                throw new BdException("Error: Connexion no activa");
            } 
        }catch (SQLException e) {
            throw new BdException("Error en BD: "+e);
        }finally{
            connection.close();
        }  
    }
    /***************************************************************************/
    /******************************** TOOLBAR **********************************/
    /***************************************************************************/
    /**
     * 
     * @param sesion
     * @param screen
     * @return
     * @throws SQLException 
     * @throws Interfaces.BdException 
     */
    public Integer[] loadToolBar(Sesion sesion,int screen) throws SQLException, BdException {
        try{
            Bd bd = ConnBdType.open(ConnBdType.SqlServer);
            connection = bd.open();
            if (connection != null){
                StringBuilder sqlProc = new StringBuilder();
                sqlProc.append("{call sp_get_menu_top( ?, ?)}");
                CallableStatement cstmt = connection.prepareCall(sqlProc.toString());                
                cstmt.setString("@id_user"  , sesion.getUsername() );
                cstmt.setInt("@id_screen"  , screen  );
                ResultSet rs = cstmt.executeQuery(); 
                List<Integer> list = new ArrayList<>();
                int i = 1;
                while(rs.next()) {
                   list.add(rs.getInt(i));
                   i++;
                }               
                return list.toArray(new Integer[list.size()]);                                    
            }else{
                throw new BdException("Error: Connexion no activa");
            } 
        }catch (SQLException e) {
            throw new BdException("Error en BD: "+e);
        }finally{
            connection.close();
        }  
    }        
    /***************************************************************************/
    /****************************** PRINT SCREEN *******************************/
    /***************************************************************************/    
    /**
     * @author MITM
     * @param idscreen
     * @param idobjeto
     * @return 
     * @throws java.sql.SQLException 
     * @throws Interfaces.BdException 
     */
    public ItemPrintScreen[] findPrintScreen(int idscreen, String idobjeto) throws SQLException, BdException {
        try{
            Bd bd = ConnBdType.open(ConnBdType.SqlServer);
            connection = bd.open();
            if (connection != null){
                StringBuilder sqlProc = new StringBuilder();
                sqlProc.append("{call sp_get_menu_print(?,?)}");
                CallableStatement cstmt = connection.prepareCall(sqlProc.toString());
                cstmt.setInt("@idscreen"    , idscreen);
                cstmt.setString("@idobjeto" , idobjeto);
                ResultSet result = cstmt.executeQuery();            
                List<ItemPrintScreen> list = new ArrayList<>();
                while(result.next()) {
                    ItemPrintScreen obj = new ItemPrintScreen();
                    obj.loadResultSet(result);
                    list.add(obj);
                }               
                return list.toArray(new ItemPrintScreen[list.size()]);     
            }else{
                throw new BdException("Error: Connexion no activa");
            } 
        }catch (SQLException e) {
            throw new BdException("Error en BD: "+e);
        }finally{
            connection.close();
        }  
    } 
    /***************************************************************************/
    /******************************* SINGLETON *********************************/
    /***************************************************************************/    
    private static class BdHolder {
        private static final BdGui INSTANCE = new BdGui();
    }    
    public static BdGui getInstance() {
        return BdGui.BdHolder.INSTANCE;
    }
}
